package com.hsy.crm.web.service.impl;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Product;
import com.hsy.crm.web.domain.entity.ProductType;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.product.ProductypeSelectPo;
import com.hsy.crm.web.domain.vo.product.ProductypeSelectVo;
import com.hsy.crm.web.mapper.ProductMapper;
import com.hsy.crm.web.mapper.ProductTypeMapper;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.UuidUtils;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Product;
import com.hsy.crm.web.domain.entity.ProductType;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.product.ProductypeSelectPo;
import com.hsy.crm.web.domain.vo.product.ProductypeSelectVo;
import com.hsy.crm.web.mapper.ProductMapper;
import com.hsy.crm.web.mapper.ProductTypeMapper;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductTypeService extends BaseService<ProductType> {

    @Autowired
    ProductTypeMapper productTypeMapper;

    @Autowired
    ProductMapper productMapper;

    public Unified<List<ProductypeSelectPo>> find(ProductypeSelectVo productypeSelectVo) {
        Unified<List<ProductypeSelectPo>> unified = new Unified<>();
        unified.setData(productTypeMapper.find(productypeSelectVo));
        return unified;
    }

    public Unified<Integer> insertProductType(ProductType productType) {
        Unified<Integer> unified = new Unified<>();
        checkInsert(productType);

        productType.setId(UuidUtils.getUuid());
        productType.setCreateTime(DateUtils.millis());
        productType.setCreateUserId(productType.getLoginUserId());
        if(productType.getParentId()==null){
            productType.setParentId(-1L);
        }

        unified.setData(productTypeMapper.insertProductType(productType));
        return unified;
    }



    public Unified<Integer> deleteProductType(ProductType productType) {
        Unified<Integer> unified = new Unified<>();

        int result = deleteCheck(productType);
        if( result != App.Result.SUCCESS ){
            unified.setResult(result);
            return unified;
        }
        this.delete(productType);
        return unified;
    }


    public Unified<Integer> updateProductType(ProductType productType) {
        checkUpdate(productType);
        return Unified.newInstance(update(productType));
    }

    private void checkInsert(ProductType productType){
        //check name
        ProductType checkType = new ProductType();
        checkType.setParentId(productType.getParentId());
        checkType.setName(productType.getName());
        checkType.setCustomerId(productType.getCustomerId());
        checkType.setCompanyId(productType.getLoginCompanyId());
        int count = productTypeMapper.selectCount(checkType);
        if( count > 0 ){
            throw new ServiceException(App.BussError.LEVEL_NAME_REPEAT.getCode());
        }
    }

    private void checkUpdate(ProductType productType){
        Integer count = productTypeMapper.updateCheck(productType);
        if( count != null ){
            throw new ServiceException(App.BussError.LEVEL_NAME_REPEAT.getCode());
        }
    }

    private int deleteCheck(ProductType productType){

        // has children
        ProductType checkType = new ProductType();
        checkType.setParentId(productType.getId());
        checkType.setCustomerId(productType.getCustomerId());
        int count = productTypeMapper.selectCount(checkType);
        if( count > 0 ){
            return App.BussError.DELETE_CHILD.getCode();
        }

        // has product
        Product product = new Product();
        product.setProductTypeId(productType.getId());
        product.setCustomerId(productType.getCustomerId());
        count = productMapper.selectCount(product);
        if( count > 0 ){
            return App.BussError.PRODUCT_TYPE_DELETE_PRODUCT.getCode();
        }

        return App.Result.SUCCESS;
    }

}
